package com.weixin.engine.config.time;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * LocalDateTime序列化为时间戳
 *
 * @author admin
 */
public class LocalDateTimeSerializer2 extends JsonSerializer<LocalDateTime> {
    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException {
        if (value != null) {
            long timestamp = LocalDateTimeUtil.toEpochMilli(value);
            gen.writeNumber(timestamp);
        }
    }
}